package com.liuqi.business.controller.admin;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.liuqi.base.BaseController;
import com.liuqi.business.dto.WalletDto;
import com.liuqi.business.dto.stat.RechargeExtractStatDto;
import com.liuqi.business.dto.stat.ReleaseStatDto;
import com.liuqi.business.dto.stat.UserStatDto;
import com.liuqi.business.dto.stat.WalletStatDto;
import com.liuqi.business.mapper.StatMapper;
import com.liuqi.business.model.CurrencyModel;
import com.liuqi.business.model.CurrencyModelDto;
import com.liuqi.business.service.CurrencyService;
import com.liuqi.business.service.UserAuthService;
import com.liuqi.business.service.UserService;
import com.liuqi.response.ReturnResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/admin/stat")
public class AdminStatController extends BaseController {

    @Autowired
    private StatMapper statMapper;
    @Autowired
    private CurrencyService currencyService;
    /*********************************************************************************************/

    /**
     * 用户新增统计
     *
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/toUser")
    public String toUser(ModelMap modelMap) {
        return "admin/stat/user";
    }

    @RequestMapping(value = "/userStat")
    @ResponseBody
    public ReturnResponse userStat(@RequestParam(value = "startDate", required = false) Date startDate,
                                   @RequestParam(value = "endDate", required = false) Date endDate) throws Exception {
        if (startDate == null) {
            startDate = DateUtil.beginOfDay(DateTime.now().offset(DateField.DAY_OF_YEAR, -7));
        }
        if (endDate == null) {
            endDate = DateUtil.endOfDay(new Date());
        }
        List<UserStatDto> list = statMapper.userStat(startDate, endDate);
        return ReturnResponse.backSuccess(list);
    }
    /*********************************************************************************************/
    /**
     * 充值统计
     *
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/toRecharge")
    public String toRecharge(ModelMap modelMap) {
        return "admin/stat/recharge";
    }

    @RequestMapping(value = "/rechargeStat")
    @ResponseBody
    public ReturnResponse rechargeStat(@RequestParam(value = "startDate", required = false) Date startDate,
                                       @RequestParam(value = "endDate", required = false) Date endDate) throws Exception {
        //7天前的时间
        if (startDate == null) {
            startDate = DateUtil.beginOfDay(DateTime.now().offset(DateField.DAY_OF_YEAR, -7));
        }
        if (endDate == null) {
            endDate = DateUtil.endOfDay(new Date());
        }
        List<RechargeExtractStatDto> list = statMapper.rechargeStat(startDate, endDate);
        list.stream().forEach(dto -> {
            CurrencyModel model = currencyService.getById(dto.getCurrencyId());
            dto.setCurrencyName(model != null ? model.getName() : "");
        });
        return getJsonData(startDate, endDate, list);
    }
    /*********************************************************************************************/
    /**
     * 提现统计
     *
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/toExtract")
    public String toExtract(ModelMap modelMap) {
        return "admin/stat/extract";
    }

    @RequestMapping(value = "/extractStat")
    @ResponseBody
    public ReturnResponse extractStat(@RequestParam(value = "startDate", required = false) Date startDate,
                                      @RequestParam(value = "endDate", required = false) Date endDate) throws Exception {
        //7天前的时间
        if (startDate == null) {
            startDate = DateUtil.beginOfDay(DateTime.now().offset(DateField.DAY_OF_YEAR, -7));
        }
        if (endDate == null) {
            endDate = DateUtil.endOfDay(new Date());
        }
        List<RechargeExtractStatDto> list = statMapper.extractStat(startDate, endDate);
        list.stream().forEach(dto -> {
            CurrencyModel model = currencyService.getById(dto.getCurrencyId());
            dto.setCurrencyName(model != null ? model.getName() : "");
        });
        return getJsonData(startDate, endDate, list);
    }

    private ReturnResponse getJsonData(Date startDate, Date endDate, List<RechargeExtractStatDto> list) {
        String dateFormat = "yyyy-MM-dd";

        List<String> dateList = new ArrayList<String>();
        List<CurrencyModelDto> currencyList = currencyService.getAll();
        Map<String, List<BigDecimal>> dataMap = new HashMap<String, List<BigDecimal>>();
        Long count = DateUtil.between(startDate, endDate, DateUnit.DAY);
        if (list != null && list.size() > 0) {
            Map<String, BigDecimal> dataTempMap = list.stream().collect(Collectors.toMap(RechargeExtractStatDto -> DateTime.of(RechargeExtractStatDto.getDate()).toString(dateFormat) + "_" + RechargeExtractStatDto.getCurrencyId(), RechargeExtractStatDto -> RechargeExtractStatDto.getTotal()));

            String dateStr = "";
            //组装数据
            for (int i = 0; i < count; i++) {
                dateStr = DateTime.of(startDate).offset(DateField.DAY_OF_YEAR, 1).toString(dateFormat);
                //日期
                dateList.add(dateStr);
                String tempKey = "";
                for (CurrencyModelDto dto : currencyList) {
                    //判断币种名称存不存在  不存在插入一条新数据
                    if (!dataMap.containsKey(dto.getName())) {
                        dataMap.put(dto.getName(), new ArrayList<BigDecimal>());
                    }
                    tempKey = dateStr + "_" + dto.getId();
                    //存在key
                    if (dataTempMap.containsKey(tempKey)) {
                        dataMap.get(dto.getName()).add(dataTempMap.get(tempKey));
                    } else {
                        dataMap.get(dto.getName()).add(BigDecimal.ZERO);
                    }
                }
            }
        }
        JSONObject obj = new JSONObject();
        obj.put("date", dateList);
        obj.put("data", dataMap);
        obj.put("list", list);
        return ReturnResponse.backSuccess(obj);
    }

    /*********************************************************************************************/


    /**
     * 钱包总统计
     *
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/toWallet")
    public String toWallet(ModelMap modelMap) {
        return "admin/stat/wallet";
    }

    @RequestMapping(value = "/walletStat")
    @ResponseBody
    public ReturnResponse walletStat() throws Exception {
        Map<Long, WalletStatDto> map = new HashMap<>();
        List<CurrencyModelDto> cList = currencyService.getAll();
        List<WalletDto> using = statMapper.getUsing();
        Map<Long, WalletDto> usingMap = using.stream().collect(Collectors.toMap(t -> t.getCurrencyId(), t -> t));
        List<WalletDto> locking = statMapper.getLock();
        Map<Long, WalletDto> lockingMap = locking.stream().collect(Collectors.toMap(t -> t.getCurrencyId(), t -> t));
        Long currencyId = 0L;
        WalletDto temWallet = null;
        for (CurrencyModelDto currency : cList) {
            currencyId = currency.getId();
            WalletStatDto temp = map.get(currencyId);
            if (temp == null) {
                temp = new WalletStatDto();
                temp.setCurrencyId(currencyId);
                temp.setCurrencyName(currency.getName());
                map.put(currencyId, temp);
            }
            //可用
            temWallet = usingMap.get(currencyId);
            if (temWallet != null) {
                temp.setUsing(temWallet.getUsing());
                temp.setFreeze(temWallet.getFreeze());
            }
            //锁仓
            temWallet = lockingMap.get(currencyId);
            if (temWallet != null) {
                temp.setLocking(temWallet.getUsing());
                temp.setLockFreeze(temWallet.getFreeze());
            }

        }

        return ReturnResponse.backSuccess(map.values());
    }


    /**
     * 钱包总统计
     *
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/toRelease")
    public String toRelease(ModelMap modelMap) {
        return "admin/stat/release";
    }

    @RequestMapping(value = "/releaseStat")
    @ResponseBody
    public ReturnResponse releaseStat() throws Exception {
        List<ReleaseStatDto> list = statMapper.releaseStat();
        return ReturnResponse.backSuccess(list);
    }
}
